﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NewsMine.Storage
{
    public class KeyValueStoreManagerOnFileSystem : NewsMine.Storage.IKeyValueStoreManager
    {
        public void CreateKeyValueStore(string name)
        {
            ValidateKeyValueStoreName(name);

            string keyVlaueDirectoryName = GetKeyValueFolderByName(name);

            if (Directory.Exists(keyVlaueDirectoryName))
            {
                throw new ApplicationException("Already this queuest is exists");
            }

            Directory.CreateDirectory(keyVlaueDirectoryName);
        }

        private string GetKeyValueFolderByName(string name)
        {
            return Path.Combine(Configuration.ConfigurationManager.KeyValueStoreBaseFolder, name);
        }

        private void ValidateKeyValueStoreName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(string.Format("Invalid keyvalue store  name {0}, Please provide the valid key value store name", name));
            }
        }

        public void DeleteKeyValueStore(string name)
        {
            ValidateKeyValueStoreName(name);

            string keyVlaueDirectoryName = GetKeyValueFolderByName(name);

            if (!Directory.Exists(keyVlaueDirectoryName))
            {
                throw new ApplicationException("The specified store is not exists on the file system.");
            }

            Directory.Delete(keyVlaueDirectoryName,true);
        }

        public string[] GetListOfKeyValueStore()
        {
            var subDirs = Directory.GetDirectories(Configuration.ConfigurationManager.KeyValueStoreBaseFolder);

            return subDirs;
        }

        public int GetItemCount(string storeName)
        {
            throw new ApplicationException();
        }


        public IKeyValueStore GetKeyValueStoreByName(string name)
        {
            ValidateKeyValueStoreName(name);

            string keyVlaueDirectoryName = GetKeyValueFolderByName(name);

            if (!Directory.Exists(keyVlaueDirectoryName))
            {
                return null;
            }

            //return new KeyValueStoreOnFileSystem(keyVlaueDirectoryName);
            return new SQLite.SQLiteKeyValueStore(keyVlaueDirectoryName);

        }

        string defaultStoreName = "DefaultStore";

        public IKeyValueStore GetDefaultQueue()
        {
            CreateDefaultStoreIfNotCreated();
            return GetKeyValueStoreByName(defaultStoreName);
        }

        private void CreateDefaultStoreIfNotCreated()
        {
            string name = defaultStoreName;

            string keyVlaueDirectoryName = GetKeyValueFolderByName(name);

            if (Directory.Exists(keyVlaueDirectoryName))
            {
                return;
            }

            Directory.CreateDirectory(keyVlaueDirectoryName);
        }
    }
}
